package com.gp.study.mybatis.interceptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.gp.study.mybatis.plugin.Interceptor;

/**
 * 利用 List 实现 伪 责任链模式
 * @author USER
 *
 */
public class InterceptorChain {

	private final List<Interceptor> interceptors = new ArrayList<Interceptor>();

	public Object pluginAll(Object target) {
		for (Interceptor interceptor : interceptors) {
			target = interceptor.plugin(target);
		}
		return target;
	}

	public void addInterceptor(Interceptor interceptor) {
		interceptors.add(interceptor);
	}

	public List<Interceptor> getInterceptors() {
		return Collections.unmodifiableList(interceptors);
	}

}
